﻿using Autofac;
using CSRedis;
using SH.Common;
using System.Reflection;

namespace SH.Multitenant.API.Modules
{
    public class AutofacConfig : Autofac.Module
    {
        protected override void Load(ContainerBuilder builder)
        {
            //通过反射的方式加载类库（程序集）
            //Assembly
            //var iRep = Assembly.Load("AsyncProgramming.IRepository");
            var rep = Assembly.Load("SH.Multitenant.Infrastructure");
            //var iSer = Assembly.Load("AsyncProgramming.IServices");
            var ser = Assembly.Load("SH.Multitenant.Application");
            //将程序集注入到AutoFac中
            //AsImplementedInterfaces()自动注册该实现类对应的接口
            builder.RegisterAssemblyTypes(rep).Where(x => x.Name.EndsWith("Repository")).AsImplementedInterfaces();
            builder.RegisterAssemblyTypes(ser).Where(x => x.Name.EndsWith("Services")).AsImplementedInterfaces();


            builder.RegisterInstance(new CSRedisClient(AppService.Configuration["redisIP"])).SingleInstance().PropertiesAutowired();
            

            //注入单个文件
            //builder.RegisterType<ProjectDbContext>();

            base.Load(builder);
        }
    }
}
